package com.maotouying.owlextremeapp.fragment;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.SlidingTabLayout;
import com.google.gson.Gson;
import com.maotouying.owlextremeapp.R;
import com.maotouying.owlextremeapp.activity.LoginActivity;
import com.maotouying.owlextremeapp.adapter.HomeAdapter;
import com.maotouying.owlextremeapp.api.Api;
import com.maotouying.owlextremeapp.api.ApiConfig;
import com.maotouying.owlextremeapp.api.TitleCallback;
import com.maotouying.owlextremeapp.entity.CategoryEntity;
import com.maotouying.owlextremeapp.entity.VideoCategoryResponse;
import com.maotouying.owlextremeapp.utils.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class HomeFragment extends BaseFragment {

    private ArrayList<Fragment> mFragments = new ArrayList<>();

    private String[] mTitles;

    private ViewPager viewPager;
    private SlidingTabLayout slidingTabLayout;

    public HomeFragment() {
    }

    public static HomeFragment newInstance() {
        HomeFragment fragment = new HomeFragment();
        return fragment;
    }

    @Override
    protected int initLayout() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {

        viewPager = mRootView.findViewById(R.id.fixedViewPager);
        slidingTabLayout = mRootView.findViewById(R.id.slidingTabLayout);
    }

    @Override
    protected void initDate() {
        getVideoCategoryLIst();
    }

    // 加载接口获取内容
    private void getVideoCategoryLIst() {
        String token = getStringFromSp("token");
        if (!StringUtils.isEmpty(token)) {
            HashMap<String, Object> params = new HashMap<>();
            params.put("token", token);
            Api.config(ApiConfig.VIDEO_CATEGORY_LIST, params).getRequest(new TitleCallback() {
                @Override
                public void onSuccess(String res) {
                    final VideoCategoryResponse response = new Gson().fromJson(res, VideoCategoryResponse.class);
                    if (response != null && response.getCode() == 0) {
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                List<CategoryEntity> list = response.getPage().getList();
                                if (null != list && list.size() > 0) {
                                    mTitles = new String[list.size()];
                                    for (int i = 0; i < list.size(); i++) {
                                        mTitles[i] = list.get(i).getCategoryName();
                                        // 从网络获取的标题赋值给mTitles后，添加到mFragments
                                        mFragments.add(VideoFragment.newInstance(list.get(i).getCategoryId()));
                                    }
                                    // 预加载顶部导航栏下标
                                    viewPager.setOffscreenPageLimit(mFragments.size());
                                    viewPager.setAdapter(new HomeAdapter(getFragmentManager(), mTitles, mFragments));
                                    slidingTabLayout.setViewPager(viewPager);
                                }
                            }
                        });
                    }
                }

                @Override
                public void onFailure(Exception e) {

                }
            });
        } else {
            navigateTo(LoginActivity.class);
        }
    }
}